package longrise.phone.com.bjjt_henan.main;

import android.content.Context;
import android.content.Intent;
import android.location.LocationManager;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.android.FormParameter;
import com.longrise.android.FrameworkManager;
import com.longrise.android.LFView;
import com.longrise.bjjt.BaseApplication;
import com.longrise.bjjt.util.PrefUtils;
import com.longrise.bjjt.util.UiUtil;

import longrise.phone.com.bjjt_henan.Constant.MsgConstant;
import longrise.phone.com.bjjt_henan.R;
import longrise.phone.com.bjjt_henan.Utils.ApiImpI;
import longrise.phone.com.bjjt_henan.Utils.DialogUtils;
import longrise.phone.com.bjjt_henan.compensate.OpenGpsView;
import longrise.phone.com.bjjt_henan.compensate.SafeWarningView;
import longrise.phone.com.bjjt_henan.myaccident.AccidentMainView;

/**
 * 快处快赔项页面
 */
public class NewKckpView extends LFView implements View.OnClickListener {

    private View mView;

    private Context mContext;

    private LinearLayout instrLayout;

    private LinearLayout kckpLayout;

    private LinearLayout caseLayout;

    private EntityBean bindBean;//绑定数据

    private int bindState;//绑定状态

    private boolean isClick = false;

    private String redes;

    private RollImageView rollImageView;

    public NewKckpView(Context context) {
        super(context);
        this.mContext = context;
    }

    public void setBindBean(EntityBean bindBean) {
        this.bindBean = bindBean;
    }

    public void setBindState(int bindState) {
        this.bindState = bindState;
        if (isClick && bindState == 1) {
            isClick = false;
            checkData();
        }
    }

    public void setRedes(String redes) {
        this.redes = redes;
    }

    @Override
    public View getView() {
        return mView;
    }

    @Override
    public FormParameter getFormParameter() {
        return null;
    }

    @Override
    public void init() {
        if (mContext == null) {
            return;
        }
        mView = LayoutInflater.from(mContext).inflate(R.layout.view_newkckp, null);
        if (mView == null) {
            return;
        }
        instrLayout = (LinearLayout) mView.findViewById(R.id.newkckp_layout_instructions);
        kckpLayout = (LinearLayout) mView.findViewById(R.id.newkckp_layout_kckp);
        caseLayout = (LinearLayout) mView.findViewById(R.id.newkckp_layout_case);
        initRollView();

        setListen(true);
    }

    //轮播图片界面
    private void initRollView() {
        LinearLayout imgLayout = (LinearLayout) mView.findViewById(R.id.newkckp_layout_imgs);
        if (rollImageView == null) {
            rollImageView = new RollImageView(mContext);
            rollImageView.setNewsType("2");
            rollImageView.init();
            rollImageView.refresh();
        }
        imgLayout.removeAllViews();
        imgLayout.addView(rollImageView.getView(), new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT));
    }

    private void setListen(boolean flag) {
        if (instrLayout != null) {
            instrLayout.setOnClickListener(flag ? this : null);
        }
        if (kckpLayout != null) {
            kckpLayout.setOnClickListener(flag ? this : null);
        }
        if (caseLayout != null) {
            caseLayout.setOnClickListener(flag ? this : null);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.newkckp_layout_instructions://使用说明
                Intent intent = new Intent(mContext, UserGuideActivity.class);
                intent.putExtra("title", mContext.getString(R.string.instructions));
                intent.putExtra("url", UserGuideActivity.INSTRUCTIONS);
                mContext.startActivity(intent);
                break;
            case R.id.newkckp_layout_case://历史案件
                PrefUtils.setBoolean(mContext, MsgConstant.HISTORY_CASE, true);
                AccidentMainView accidentView = new AccidentMainView(mContext);
                FrameworkManager.getInstance().showNewForm(mContext, accidentView);
                break;
            case R.id.newkckp_layout_kckp://快处快赔
                checkTime();
                break;
            default:
                break;
        }
    }

    private void checkData() {
        if (bindState == -1) {//查询数据失败则重新查询
            LSMsgCall(99, "bindinfo");
            isClick = true;//点击后如果数据加载正确则直接进入下一个界面
            return;
        }
        //判断是否打开gps，没有打开跳转到提示打开gps界面，已经打开进入安全提示,获取areaid并存入application中
        if (bindState == 1) {//已经绑定信息，进入下个流程
            PrefUtils.setBoolean(mContext, MsgConstant.HISTORY_CASE, false);
            LocationManager locationManager = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);
            if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
                //跳转到提示打开gps界面
                OpenGpsView opengpsview = new OpenGpsView(mContext);
                FrameworkManager.getInstance().showNewForm(mContext, opengpsview);
            } else {
                //跳转到安全提示界面
                SafeWarningView safewarningview = new SafeWarningView(mContext);
                FrameworkManager.getInstance().showNewForm(mContext, safewarningview);
            }
        } else {
            //没有绑定信息，提示先绑定信息
            if (TextUtils.isEmpty(redes)) {
                redes = "请先绑定驾驶证和机动车信息！";
            }
            UiUtil.showToast(mContext, redes);
        }
    }

    /**
     * 判断是否在允许的时间内
     */
    private void checkTime() {
        DialogUtils.showDialog(mContext);
        String userflag = ((BaseApplication) mContext.getApplicationContext()).getKckpName();
        String token = ((BaseApplication) mContext.getApplicationContext()).getToken();
        ApiImpI.getInstance(mContext).checkTime(userflag, token, new ApiImpI.RequestCallBack() {
            @Override
            public void onError(String error) {
                DialogUtils.closeDialog();
                if (!TextUtils.isEmpty(error)) {
                    UiUtil.showToast(mContext, error);
                }
            }

            @Override
            public void onSuccess(Object result) {
                DialogUtils.closeDialog();
                EntityBean bean = (EntityBean) result;
                String restate = bean.getString("restate");
                if (TextUtils.equals("1", restate)) {
                    EntityBean dataBean = bean.getBean("data");
                    if (null != dataBean) {
                        String isTrue = dataBean.getString("istrue");
//                        if (TextUtils.equals("1", isTrue)) {
                        checkData();
                        return;
//                        }
                    }
                }
                String redes = bean.getString("redes");
                UiUtil.showToast(mContext, redes);
            }
        });
    }

    @Override
    public void refreshByTime() {

    }

    @Override
    public void refresh() {

    }

    @Override
    public void OnDestroy() {
        super.OnDestroy();
        setListen(false);
        if (null != rollImageView) {
            rollImageView.OnDestroy();
            rollImageView = null;
        }
        mView = null;
    }
}
